package cn.nsu.douban.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.nsu.douban.dao.dao.IPingFenDao;
import cn.nsu.douban.entity.EvaluationInfo;
import cn.nsu.douban.entity.MovieInfo;

@Repository
@SuppressWarnings("all")
public class IPingFenDaoImpl extends HibernateDaoSupport implements IPingFenDao {

	@Resource
	public void setSesssionFactory(SessionFactory   sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

	@Override
	public void insertEmInfoByUserIdAndMoId(EvaluationInfo emInfo) {

		  this.getHibernateTemplate().save(emInfo);
	}

	@Override
	public List<EvaluationInfo> getPingFen(MovieInfo movieInfo) {
		String hql=" select e from EvaluationInfo e join e.movieInfo m where m.id=?";
		return 	this.getHibernateTemplate().find(hql,movieInfo.getId());
	}

	@Override
	public Integer getPingFenCount(MovieInfo movieInfo) {
          
		  DetachedCriteria detachedCriteria=DetachedCriteria.forClass(EvaluationInfo.class);
		  detachedCriteria.add(Restrictions.eq("movieInfo", movieInfo));
		 detachedCriteria.setProjection(Projections.count("movieInfo"));
      List<Long> countList = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		 return  countList.get(0).intValue();
	}

	
}
